package com.wuyou.single.common.security.handler;

import com.alibaba.fastjson2.JSON;
import com.wuyou.single.common.api.CommonResult;
import com.wuyou.single.common.api.ResultCode;
import com.wuyou.single.common.util.ServletUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 退出登录
 * @Author 无忧
 * @Date 2024/6/12 23:03
 */
@Component
public class LogoutHandler implements LogoutSuccessHandler {


    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        if (StringUtils.isNotNull(loginUser)) {
//            String userName = loginUser.getUsername();
//            // 删除用户缓存记录
//            tokenService.delLoginUser(loginUser.getToken());
//            // 记录用户推出日志
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "退出成功"));
//        }
        // 1、将token失效

        // 2、进行登出

        ServletUtils.renderString(response, JSON.toJSONString(CommonResult.success(ResultCode.SUCCESS, "退出成功")));
    }
}
